<?php

namespace App\Http\Controllers\Home;

use App\Models\Comment;
use App\Models\Goods;
use App\Models\Goodsimg;
use Illuminate\Http\Request;

//使用类
use App\Http\Controllers\Controller;

//商品页控制器
class GoodsController extends Controller
{
    //商品页
	public function index($id){
        //dd($id);
        //获取商品相关数据
        $goods=Goods::where("id",$id)->first();
        //商品图片表
        $goodsImg=Goodsimg::where("gid",$id)->get();

        //查询商品对应评论
        $commentTot=Comment::where([["gid",$id],["statu",1]])->count();//全部
        $goodTot=Comment::where([["gid",$id],["start",">",4],["statu",1]])->count();//好评
        $chaTot=Comment::where([["gid",$id],["start","<=",2],["statu",1]])->count();//差评
        $zhongTot=$commentTot-$goodTot-$chaTot;//中评

        //dd($goods);
        $arr=array(     //数组
            "commentTot"=>$commentTot,
            "goodTot"=>$goodTot,
            "chaTot"=>$chaTot,
            "zhongTot"=>$zhongTot,
        );

        //dd($arr);

        $comment=Comment::where([["gid",$id],["statu",1]])->get();

        //格式化数据
        $data=array(
            "goods"=>$goods, //对象数组？
            "goodsImg"=>$goodsImg,
            "arr"=>$arr,
            "comment"=>$comment
        );


		//加载页面

        return view("home.goods")->with($data);
	}
	
}
